package com.ruoyi.project.jiaqin.company.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jiaqin.apply.domain.CompanyApply;
import com.ruoyi.project.jiaqin.apply.service.ICompanyApplyService;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.companyindustry.domain.CompanyIndustry;
import com.ruoyi.project.jiaqin.companyindustry.service.ICompanyIndustryService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.service.IMemberService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 公司Controller
 *
 * @author admin
 * @date 2021-01-05
 */
@Controller
@RequestMapping("/jiaqin/company")
public class CompanyController extends BaseController
{
    private String prefix = "jiaqin/company";

    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private ICompanyIndustryService companyIndustryService;
    @Autowired
    private ICompanyApplyService companyApplyService;

    @RequiresPermissions("jiaqin:company:view")
    @GetMapping()
    public String company()
    {
        return prefix + "/company";
    }

    /**
     * 查询公司列表
     */
    @RequiresPermissions("jiaqin:company:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Company company)
    {
        startPage();
        List<Company> list = companyService.selectCompanyList(company);
        for (Company C : list) {
            CompanyIndustry companyIndustry = companyIndustryService.selectCompanyIndustryById(C.getIndustry());
            if (companyIndustry != null) {
                C.setIndustry(companyIndustry.getName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出公司列表
     */
    @RequiresPermissions("jiaqin:company:export")
    @Log(title = "公司", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Company company)
    {
        List<Company> list = companyService.selectCompanyList(company);
        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
        return util.exportExcel(list, "company");
    }

    /**
     * 新增公司
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存公司
     */
    @RequiresPermissions("jiaqin:company:add")
    @Log(title = "公司", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Company company)
    {
        return toAjax(companyService.insertCompany(company));
    }

    @Autowired
    private IMemberService memberService;
    /**
     * 修改公司
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Company company = companyService.selectCompanyById(id);
        mmap.put("company", company);
        return prefix + "/edit";
    }

    /**
     * 关联用户页
     */
    @GetMapping("/relation/{id}")
    public String relation(@PathVariable("id") String id, ModelMap mmap)
    {
        Company company = companyService.selectCompanyById(id);
        //查询后台用户列表
        List<User> users = iUserService.selectUserList(new User());
        mmap.put("company", company);
        mmap.put("users",users);
        return prefix + "/relation";
    }
    /**
     * 修改保存公司
     */
    @RequiresPermissions("jiaqin:company:edit")
    @Log(title = "公司", businessType = BusinessType.UPDATE)
    @PostMapping("/relation")
    @ResponseBody
    public AjaxResult relationSave(Company company)
    {
        Company company1 = companyService.selectCompanyBySysUserId(company.getSysUserId());
        if(company1 != null){
            return AjaxResult.error("一个系统用户不可绑定两个企业");
        }
        return toAjax(companyService.updateCompany(company));
    }

    /**
     * 修改保存公司
     */
    @RequiresPermissions("jiaqin:company:edit")
    @Log(title = "公司", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Company company)
    {
        Member member = new Member();
        member.setId(company.getMemberId());
        member.setNewsClose(company.getNewsClose());
        memberService.updateMember(member);
        return toAjax(companyService.updateCompany(company));
    }

    /**
     * 删除公司
     */
    @RequiresPermissions("jiaqin:company:remove")
    @Log(title = "公司", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        Company company = companyService.selectCompanyById(ids);
        CompanyApply companyApply = new CompanyApply();
        companyApply.setMemberId(company.getMemberId());
        List<CompanyApply> companyApplyList = companyApplyService.selectCompanyApplyList(companyApply);
        if (companyApplyList.size() > 0) {
            for (CompanyApply CA : companyApplyList) {
                //删除企业申请
                companyApplyService.deleteCompanyApplyByIds(CA.getId());
            }
        }
        return toAjax(companyService.deleteCompanyByIds(ids));
    }

    /**
     * 置顶
     */
    @PostMapping("/editTopping/{id}")
    @ResponseBody
    public AjaxResult editCoach(@PathVariable String id)
    {
        Company companyById = companyService.selectCompanyById(id);
        if (companyById == null) {
            return AjaxResult.error("用户异常");
        }
        Company company = new Company();
        company.setId(id);
        company.setUpdatedTime(new Date());
        companyService.updateCompany(company);
        return AjaxResult.success();
    }
}
